package jzOffer;

import java.util.LinkedList;

/**
 * @author 郝文雪
 * @date 2022/7/18 17:40
 * @Description 二叉树的下一个结点
 */
class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;

    TreeLinkNode(int val) {
        this.val = val;
    }
}
public class JZ8 {
    LinkedList<TreeLinkNode> list=new LinkedList<>();
    public TreeLinkNode GetNext(TreeLinkNode pNode) {
        TreeLinkNode root=pNode;
        while(root.next!=null) root=root.next;
        inorder(root);
        int n=list.size();
        for(int i=0;i<n-1;i++){
            if(list.get(i)==pNode){
                return list.get(i+1);
            }
        }
        return null;
    }
    private void inorder(TreeLinkNode root){
        if(root!=null){
            inorder(root.left);
            list.add(root);
            inorder(root.right);
        }
    }
}
